package com.gy.handler;

import java.io.IOException;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.gy.domain.Userinfo;
import com.gy.service.UserinfoService;

@Controller
@RequestMapping("UserinfoHandler")
public class UserinfoHandler {
	
	@Autowired
	private UserinfoService userinfoService;
	//作为指纹相对路径
	private String fileName=null;
	
	@RequestMapping(value="insertUserinfo" ,method=RequestMethod.POST)
	@ResponseBody
	public void insertUserinfo(Userinfo userinfo, BindingResult result)
	{
		//System.out.println(name);
		
		
		System.out.println("/fingerprint/"+fileName+".FBI");
		userinfo.setuFingerprint("/fingerprint/"+fileName+".FBI");
		userinfoService.insertUser(userinfo);
		fileName=null;
	}
	/**
	 * 退宿
	 * @param userinfo
	 * @param result
	 */
	@RequestMapping(value="delectUserinfo" ,method=RequestMethod.POST)
	@ResponseBody
	public void delectUserinfo(@RequestParam("uid") String id)
	{
		long idl = Long.parseLong(id);
		userinfoService.delectUser(idl);
		//System.out.println(name);
		
	}
	//返回显示用户信息
	@RequestMapping(value="selectUserinfo" ,method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
	@ResponseBody
	public String selectUserinfo(@RequestParam("uNum") String room)
	{
		//System.out.println(name);
		Userinfo user=null;
		user=userinfoService.selectUser(room);
		if(user==null)
		{
			return "0";
		}
		user.setuName(user.getuName());
		String userJson= JSON.toJSONString(user);
		//JSONObject jsonObj = JSONObject.parseObject(userJson);
		//String userJson1= JSON.toJSONString(jsonObj);
		//System.out.println(userJson);
		
		return userJson;
		//System.out.println("lala");
		//System.out.println(userinfo.toString());
	}
	//指纹录入
	@RequestMapping(value = "fingerprint", method = RequestMethod.POST)
	@ResponseBody
	public String fingerprint(@RequestParam("bz") String b,@RequestParam(value="name",required=false) String name) {
		int bz=Integer.parseInt(b);
		//fileName=name;
		//UUID uuid = UUID.randomUUID();
		fileName=UUID.randomUUID().toString().replaceAll("-", "");
		System.out.println(bz);
		System.out.println(fileName);
		// 调用python
		try {
			Process ps = null;
			int status1=10,status2=10,status3=10;
			String path=System.getProperty("evan.webapp");
			System.out.println(path);
			if(bz==0)
			{
				return "0";
			}
			if (bz == 1) {
				
				ps = Runtime.getRuntime().exec("python "+path+"/zw/getImage.py");
				status1 = ps.waitFor();//返回0为成功
				ps = Runtime.getRuntime().exec("python "+path+"/zw/ganchar_1.py");
				status2 = ps.waitFor();//返回0为成功
				if(status1==0&&status2==0)
				{
					status1=10;
					status2=10;
					return "1";
				}
				else
				{
					status1=10;
					status2=10;
					return "-1";
				}
			}
			
			if (bz == 2) {
				ps = Runtime.getRuntime().exec("python "+path+"/zw/getImage.py");
				status1 = ps.waitFor();//返回0为成功
				ps = Runtime.getRuntime().exec("python "+path+"/zw/ganchar_2.py");
				status2 = ps.waitFor();//返回0为成功
				if(status1==0&&status2==0)
				{
					status1=10;
					status2=10;
					return "2";
				}
				else
				{
					status1=10;
					status2=10;
					return "-2";
				}
			}
			
			if (bz == 3) {
				ps = Runtime.getRuntime().exec("python "+path+"/zw/regmodel.py");
				status1 = ps.waitFor();//返回0为成功
				ps = Runtime.getRuntime().exec("python "+path+"/zw/storeChar.py");
				status2 = ps.waitFor();//返回0为成功
				String[] args = new String[] { "python",path+"/zw/upload.py",path,fileName};
				ps = Runtime.getRuntime().exec(args);
				status3 = ps.waitFor();//返回0为成功
				if(status1==0&&status2==0&&status3==0)
				{
					status1=10;
					status2=10;
					status3=10;
					return "3";
				}
				else
				{
					status1=10;
					status2=10;
					status3=10;
					return "-3";
				}
			}
			
		} catch (IOException | InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return "9";
	}
}
